package com.gopay.bis.bocom.ebank.operation;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;

import com.bankcomm.netpay.Config;
import com.bankcomm.netpay.Post;
import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.TransUtils;
import com.gopay.common.constants.bank.BocomConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.bocom.ebank.pay.BocomB2bQueryDetail;
import com.gopay.common.domain.bank.bocom.ebank.pay.b2b.req.BocomB2bReq;
import com.gopay.common.exception.GopayException;

public class BocomB2bQuery {


	private String filePath;

	/**
	 * 交行B2B查询
	 * 
	 * @param BocomMessageB2B
	 * @return BocomMessageB2B
	 */
	
	public BocomB2bReq query(BaseMessage message) throws GopayException{
		try{
			String configID ="b2bpay";
			Config.loadConfig(filePath);
			String reqSrc= "<?xml version=\"1.0\" encoding=\"gb2312\"?>"+
					"<Document>"+
					"<Head>"+
					"<TranCode>NPAY1009</TranCode>"+
					"<MerchNo>"+Config.getMerID(configID)+"</MerchNo>"+
					"</Head>"+
					"<Body>"+
					"<OrderNo>"+message.getOrderId()+"</OrderNo>"+
					"<TranMode>2</TranMode>"+
					"<TranType>0</TranType>"+
					"</Body>"+
					"</Document>";
			BankLogUtils.printB2BQuerySendLog(message, " BOCOM B2B query orderid:"+ reqSrc);
			String reqSign=Config.attachedSign(configID,reqSrc);
			String resSign=Post.post(Config.getHttpsType(configID),Config.getApiURL(configID),"reqData="+java.net.URLEncoder.encode(reqSign, "GBK"));
			String resSrc = Config.attachedVerify(configID,resSign );
			BankLogUtils.printB2BQueryBlackLog(message, resSrc);
			
			BocomB2bReq b2b =new BocomB2bReq();
			if(resSrc == null){
				b2b.setErrorMessage(message.getOrderId() + "bocom b2b query return null");
				BankLogUtils.printB2BQueryBlackLog(b2b, ToStringBuilder.reflectionToString(b2b));
                return b2b;
			}
			
			//响应状态
			String rspType = "";
			String body = "";
			
			if(!resSrc.contains("<RspType>")){
				//对账响应类型为空
				b2b.setRetCode("E");
				b2b.setErrorMessage("bocom b2b query return RspType is null");
				BankLogUtils.printB2BQueryBlackLog(message, ToStringBuilder.reflectionToString(b2b));
                return b2b;
			}
			
			rspType = TransUtils.getMsgField("<RspType>", "</RspType>", resSrc);
			b2b.setRetCode(rspType);
			if(!BocomConstants.RSPTYPE_SUCCESS.equals(rspType)){
				b2b.setRetCode("E");
				b2b.setErrorMessage("bocom b2b query return RspType is error");
			} else {
				body = TransUtils.getMsgField("<Body>", "</Body>", resSrc);
			}
			if(!"".equals(body)){
				List<BocomB2bQueryDetail> details =new ArrayList<BocomB2bQueryDetail>();
				BocomB2bQueryDetail detail =new BocomB2bQueryDetail();
				detail.setOrder(TransUtils.getMsgField("<OrderNo>", "</OrderNo>", resSrc));
				detail.setAmount(TransUtils.getMsgField("<Amount>", "</Amount>", resSrc));
				detail.setOrderState(TransUtils.getMsgField("<TranState>", "</TranState>", resSrc));
				details.add(detail);
				b2b.setQueryDetails(details);
			}
			BankLogUtils.printB2BQueryBlackLog(message, ToStringBuilder.reflectionToString(b2b));
			return b2b;
		
		}catch(Exception e){
			BankLogUtils.printB2BQueryExceptionLog(message, e);
    		throw new GopayException(ProcCodeConstants.PROC_CODE_100E6036);
		}  
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	

}
